package com.test.thread.virtualthread;

import jdk.internal.vm.Continuation;

import java.time.Duration;
import java.util.Timer;
import java.util.TimerTask;

public class WaitingOperation {

    public static void perform(String name, Duration duration) {
        System.out.println("Waiting for " + name + " for " + duration.getSeconds() + " seconds");

        var vt = VirtualThreadScheduler.CURRENT_VIRTUAL_THREAD.get();
        var timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Demo.SCHEDULER.schedule(vt);
                timer.cancel();
            }
        }, duration.toMillis());
        Continuation.yield(VirtualThread.SCOPE);
    }
}
